#! /usr/bin/env bash
#
# Run the statistics for all the languages instead of just one, in parallel.
# Any argument is optional and will be forwarded to the run-lang script.
#
set -eu -o pipefail

forwarded_option_list=''

usage() {
  cat <<EOF
Usage: ./run-all [OPTIONS]

Run parsing stats for all the languages supported by semgrep-core.

Options:
  --help
     Show this message and exit.
  --upload
     Upload the stats to the semgrep dashboard as soon as they're available
     for each language.
EOF
}

while [[ $# != 0 ]]; do
  case "$1" in
    --upload)
      forwarded_option_list+=" --upload"
      ;;
    --help)
      usage
      exit 0
      ;;
    *)
      error "Unsupported command-line argument: $1"
  esac
  shift
done

pid_map=pids
rm -f "$pid_map"

# Run the parsing jobs in parallel. Output may be mangled.
pid_list=''
for dir in lang/*; do
  lang=$(basename "$dir")

  ./run-lang "$lang" $forwarded_option_list &

  pid=$!
  pid_list+=" $pid"
  echo "$pid $lang" >> "$pid_map"
done

success=true
exit_statuses=exit-statuses
rm -f "$exit_statuses"

for pid in $pid_list; do
  # Tolerate failures so as to not block the other jobs
  lang=$(grep "$pid" "$pid_map" | cut -f2)
  if wait "$pid"; then
    echo "OK $lang" >> "$exit_statuses"
  else
    echo "FAIL $lang" >> "$exit_statuses"
    success=false
  fi
done
rm -f "$pid_map"

# Show all the results sequentially at the end
for dir in lang/*; do
  lang=$(basename "$dir")
  echo "------ $lang -----"
  if [[ -e "$dir"/stats.json ]]; then
    cat "$dir"/stats.json
  fi
done

# Same but shorter
for dir in lang/*; do
  lang=$(basename "$dir")
  jsonfile="$dir"/stats.json
  if [[ -e "$jsonfile" && -n "$(cat "$jsonfile")" ]]; then
    echo "Language: $lang"
    echo "Line count: $(jq .global.line_count "$jsonfile")"
    echo "Parsing rate: $(jq .global.parsing_rate "$jsonfile")"
  else
    echo "Language: $lang"
    echo "no data"
  fi
  echo
done

cat "$exit_statuses"
rm -f "$exit_statuses"

if [[ "$success" = false ]]; then
  echo "*** Parsing stats failed for one or more languages." >&2
  exit 1
fi
